package com.itheima.mode_test;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者，也就是微信公众号服务
 * 实现了Observerable接口，对Observerable接口的三个方法进行了具体实现
 * @date：2020/3/4 15:33
 */
public class WeChat implements Observerable {

    private String msg;
    private List<Observer> list = new ArrayList<>();

    @Override
    public void register(Observer observer) {
        list.add(observer);
    }

    @Override
    public void remove(Observer observer) {
        if (!list.isEmpty()){
            list.remove(observer);
        }
    }

    @Override
    public void adviceObserver() {
        for (Observer observer : list) {
            observer.update(msg);
        }
    }

    // 发布消息
    public void setInfo(String msg){
        this.msg = msg;
        adviceObserver();
    }
}
